package com.fjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjh.domain.Category;
import com.fjh.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "商品分类管理")
@RequestMapping("prod/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品分类
     * @return
     */
    @GetMapping("table")
    @ApiOperation("查询商品分类")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> loadCategoryPage(){
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }

    /**
     * 查询商品所有的父分类
     * @return
     */
    @GetMapping("listCategory")
    @ApiOperation("查询商品所有的父分类")
    @PreAuthorize("hasAuthority('prod:category:page')")
    public ResponseEntity<List<Category>> listCategory(){
        List<Category> list = categoryService.loadParentCategory();
        return ResponseEntity.ok(list);
    }

    @PostMapping
    @ApiOperation("新增商品分类")
    @PreAuthorize("hasAuthority('prod:category:save')")
    public ResponseEntity<Void> saveCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }
}
